#include "ASFTAboutScreen.h"
#include "ASScreenMgr.h"
#include "ASScreenShare.h"
#include "..\app\ASApp.h"
#include "..\app\ASAppConst.h"
#include "..\control\ASButton.h"
#include "..\app\ASFTData.h"
#include "..\control\ASTextView.h"
#include "..\app\ASFTIo.h"
#include "ASFTTypeScreen.h"
#include "ASCustomScreen.h"

static as_ft_about_screen* s_as_ft_about_scr_p;
static as_ft_aboutsub_screen* s_as_ft_aboutsub_scr_p;
static as_ft_about_type s_as_ft_about_type;

static void as_ft_about_screen_add_listitem(as_list* list_p, as_rect pos, ASSTR text, as_void_func func)
{
    as_listitem* li_p;
    as_label* lb_p;
    ASUINT param[4];
    li_p = list_p->add_item((as_control*)list_p, pos);

    if(li_p)
    {
        li_p->set_bg_color((as_control*)li_p, DATA_SHEET_BG_COLOR);
        li_p->set_bg_selected_image((as_control*)li_p, as_ufg_tmp(0, IMG_NML_LIST_HL), AS_IMAGE_FROM_RES);

        if(func)
            li_p->set_pen_up_cb((as_control*)li_p, (as_listitem_cb)func);

        param[0] = (ASUINT)as_ucs2_from_gb2312(text);
        lb_p = (as_label*)li_p->add_ctrl((as_control*)li_p, AS_CTRL_LABEL, pos, 1, (void**)param);

        if(lb_p)
        {
            lb_p->set_text_color((as_control*)lb_p, EDIT_TEXT_COLOR);
            lb_p->set_bg_hidden((as_control*)lb_p, ASTRUE);
            lb_p->set_layout((as_control*)lb_p, AS_LABEL_TEXT_LEFT | AS_LABEL_TEXT_V_CENTER);
        }
    }
}

void as_ft_about_enter_local_help(void)
{
    s_as_ft_about_type = AS_FT_ABOUT_LOCAL_HELP;
    as_enter_ft_aboutsub_screen();
}

void as_ft_about_enter_local_about(void)
{
    s_as_ft_about_type = AS_FT_ABOUT_LOCAL_ABOUT;
    as_enter_ft_aboutsub_screen();
}

void as_ft_about_enter_local_score(void)
{
    s_as_ft_about_type = AS_FT_ABOUT_LOCAL_SCORE;
    as_enter_ft_aboutsub_screen();
}

static void as_ft_about_screen_gen_list(as_list* list_p)
{
    as_rect pos;
    as_memcpy(&pos, &list_p->base.area, sizeof(as_rect));
    pos.h = NORMAL_LIST_ITEM_H;
    as_ft_about_screen_add_listitem(list_p, pos, TEXT_USEFUL_HELP, as_ft_about_enter_local_help);
#if defined(AS_FREE_PAY_ENABLE) || defined(AS_LOCAL_SCORE_ENABLE)
    pos.y += pos.h;
    as_ft_about_screen_add_listitem(list_p, pos, TEXT_ABOUT_SCORE, as_ft_about_enter_local_score);
#endif
    pos.y += pos.h;
    as_ft_about_screen_add_listitem(list_p, pos, TEXT_ABOUT, as_ft_about_enter_local_about);
    list_p->set_selected_idx((as_control*)list_p, 0);
}

static void as_ft_about_screen_create_list(as_screen* screen_p, as_rect area)
{
    as_list* list_p = (as_list*)screen_p->add_ctrl(screen_p, AS_CTRL_LIST, area, 0, ASNULL);

    if(!list_p)
        return;

    list_p->set_bg_color((as_control*)list_p, DATA_SHEET_BG_COLOR);
    s_as_ft_about_scr_p->list_p = list_p;
    as_ft_about_screen_gen_list(s_as_ft_about_scr_p->list_p);
}

static void as_ft_about_screen_lsk(void)
{
    if(s_as_ft_about_scr_p && s_as_ft_about_scr_p->list_p)
    {
        if(s_as_ft_about_scr_p->list_p->select_item && s_as_ft_about_scr_p->list_p->select_item->pen_up_cb)
        {
            s_as_ft_about_scr_p->list_p->select_item->pen_up_cb(s_as_ft_about_scr_p->list_p->select_item);
        }
    }
}

static void as_ft_about_screen_create_bottom_bar(as_screen* screen_p, as_rect area)
{
    as_button* btn_p;
    as_screen_create_bar_img(screen_p, area, IMG_BOTTOM);
    area.w = TEXT_SK_W;
    btn_p = as_screen_create_lsk(screen_p, area, TEXT_SELECT, as_ft_about_screen_lsk);

    if(btn_p)
        btn_p->add_keycode((as_control*)btn_p, AS_KEY_OK);

    area.w = TEXT_SK_W;
    area.x = SCR_W - area.w;
    as_screen_create_rsk(screen_p, area, TEXT_BACK, as_show_custom_screen_from_child);
}

void as_ft_about_screen_init(as_screen* screen_p)
{
    as_rect area;

    if(!screen_p)
        return;

    screen_p->set_bg_color(screen_p, DATA_SHEET_BG_COLOR);
    s_as_ft_about_scr_p = (as_ft_about_screen*)screen_p;
    area.x = 0;
    area.y = as_app_get_advbar_height();
    area.w = SCR_W;
    area.h = SMALL_TOP_BAR_H;
    as_screen_create_title_bar(screen_p, area, IMG_SMALL_TOP, IMG_TITLE_ABOUT);
    area.x += 2;
    area.w = SCR_W - area.x * 2;
    area.y += area.h;
    area.h = SCR_H - BOTTOM_BAR_HEIGHT - area.y;
    as_ft_about_screen_create_list(screen_p, area);
    area.w = SCR_W;
    area.h = BOTTOM_BAR_HEIGHT;
    area.x = 0;
    area.y = SCR_H - area.h;
    as_ft_about_screen_create_bottom_bar(screen_p, area);

    if(as_app_get_show_advbar())
    {
        area.x = area.y = 0;
        area.w = SCR_W;
        as_screen_add_advbar(screen_p, area);
    }
}
void as_ft_about_screen_deinit(as_screen* screen_p)
{
    AS_UNUSED(screen_p);
    s_as_ft_about_scr_p = ASNULL;
}

void as_enter_ft_about_screen(void)
{
    as_screen_manager* scr_mgr_p = as_get_screen_stack_instance();
    as_screen* scr_p;
    scr_mgr_p->new_and_push_screen(AS_SCREEN_FTABOUT);
    scr_p = scr_mgr_p->get_cur_screen();

    if(scr_p)
    {
        scr_p->paint_hdlr(scr_p);
        scr_p->flush_hdlr(scr_p);
    }

    as_custom_screen_set_if_goback_reload(ASFALSE);
}

static void as_ft_about_screen_create_textview(as_screen* screen_p, as_rect area, ASSTR text)
{
    ASUINT param[4];
    as_textview* tv_p;
    param[0] = (ASUINT)as_ucs2_from_gb2312(text);
    tv_p = (as_textview*)screen_p->add_ctrl(screen_p, AS_CTRL_TEXTVIEW, area, 1, (void**)param);

    if(tv_p)
    {
        tv_p->set_bg_color((as_control*)tv_p, DATA_SHEET_BG_COLOR);
        tv_p->set_bg_frame_color((as_control*)tv_p, DATA_SHEET_BG_COLOR);
        tv_p->set_sb_show((as_control*)tv_p, ASTRUE);
        //tv_p->label->set_if_small((as_control*)tv_p->label,ASTRUE);
    }
}

static void as_ft_aboutsub_screen_create_local_help(as_screen* screen_p, as_rect area)
{
    as_ft_about_screen_create_textview(screen_p, area, TEXT_HELP_CONTENT);
}

static void as_ft_aboutsub_screen_create_local_about(as_screen* screen_p, as_rect area)
{
    as_rect pos;
    as_label* ver;
    pos.w = IMG_LOGO_S_W;
    pos.h = IMG_LOGO_S_H;
    pos.x = area.x + 2;
    pos.y = area.y;
    as_screen_create_bar_img(screen_p, pos, IMG_LOGO_S);
    pos.x += pos.w;
    pos.w = area.x + area.w - pos.x;
    pos.h >>= 1;
    ver = as_screen_create_label(screen_p, pos, as_ft_get_version_str(), EDIT_TEXT_COLOR, ASFALSE);

    if(ver)
    {
        ver->set_bg_hidden((as_control*)ver, ASTRUE);
        ver->set_layout((as_control*)ver, AS_LABEL_TEXT_LEFT_BOTTOM);
    }

    pos.y += pos.h;
    ver = as_screen_create_label(screen_p, pos, TEXT_ABOUT_AUTHOR, EDIT_TEXT_COLOR, ASFALSE);

    if(ver)
    {
        ver->set_bg_hidden((as_control*)ver, ASTRUE);
        ver->set_layout((as_control*)ver, AS_LABEL_TEXT_LEFT_BOTTOM);
    }

    pos.y += pos.h;
    pos.h =  area.y + area.h - pos.y;
    pos.x = area.x + 2;
    pos.w = area.w - 4;
    as_ft_about_screen_create_textview(screen_p, pos, TEXT_ABOUT_TOPIC);
}

void as_ft_aboutsub_screen_update_local_score(void)
{
#ifdef AS_LOCAL_SCORE_ENABLE

    if(s_as_ft_aboutsub_scr_p && s_as_ft_aboutsub_scr_p->score)
    {
        ASCHAR scores[16];
        as_sprintf(scores, "%d", as_ft_get_score());
        s_as_ft_aboutsub_scr_p->score->change_text((as_control*)s_as_ft_aboutsub_scr_p->score, as_ufg_tmp(0, scores));
        //s_as_ft_aboutsub_scr_p->score->base.paint_hdlr((as_control*)s_as_ft_aboutsub_scr_p->score);
        //as_flush();
    }

#endif
}

static void as_ft_aboutsub_screen_create_local_score(as_screen* screen_p, as_rect area)
{
    ASCHAR scores[16];
    as_rect pos;
    as_memcpy(&pos, &area, sizeof(as_rect));
#ifdef AS_LOCAL_SCORE_ENABLE
    pos.w = as_get_wstr_width(as_ucs2_from_gb2312(TEXT_CURR_SCORE));
    pos.h = as_get_wstr_height(as_ucs2_from_gb2312(TEXT_CURR_SCORE));
    pos.x = area.x + 2;
    pos.y = area.y + 2;
    as_screen_create_label(screen_p, pos, TEXT_CURR_SCORE, EDIT_TEXT_COLOR, ASFALSE);
    pos.x += pos.w;
    pos.w = area.x + area.w - pos.x;
    as_sprintf(scores, "%d", as_ft_get_score());
    s_as_ft_aboutsub_scr_p->score = as_screen_create_label(screen_p, pos, scores, EDIT_TEXT_COLOR, ASFALSE);

    if(s_as_ft_aboutsub_scr_p->score)
    {
        s_as_ft_aboutsub_scr_p->score->set_layout((as_control*)s_as_ft_aboutsub_scr_p->score, AS_LABEL_TEXT_LEFT | AS_LABEL_TEXT_V_CENTER);
    }

    pos.y += pos.h + 10;
    pos.h = area.y + area.h - pos.y;
    pos.x = area.x;
    pos.w = area.w;
#endif
#if defined(AS_FREE_PAY_ENABLE) || defined(AS_LOCAL_SCORE_ENABLE)
    as_ft_about_screen_create_textview(screen_p, pos, TEXT_SCORE_RULES);
#endif
}

static void as_ft_aboutsub_screen_create_content(as_screen* screen_p, as_rect area)
{
    if(s_as_ft_about_type == AS_FT_ABOUT_LOCAL_HELP)
    {
        as_ft_aboutsub_screen_create_local_help(screen_p, area);
    }
    else if(s_as_ft_about_type == AS_FT_ABOUT_LOCAL_ABOUT)
    {
        as_ft_aboutsub_screen_create_local_about(screen_p, area);
    }
    else if(s_as_ft_about_type == AS_FT_ABOUT_LOCAL_SCORE)
    {
        as_ft_aboutsub_screen_create_local_score(screen_p, area);
    }
}

static void as_ft_aboutsub_screen_enter_shop(void)
{
    as_fttype_enter_shop_screen();
}

static void as_ft_aboutsub_screen_create_bottom_bar(as_screen* screen_p, as_rect area)
{
    as_screen_create_bar_img(screen_p, area, IMG_BOTTOM);
    area.w = TEXT_SK_W;
    area.x = SCR_W - area.w;
    as_screen_create_rsk(screen_p, area, TEXT_BACK, as_screen_go_back);

    if(s_as_ft_about_type == AS_FT_ABOUT_LOCAL_SCORE)
    {
        area.x = 0;
        as_screen_create_lsk(screen_p, area, TEXT_SCORE_SHOP, as_ft_aboutsub_screen_enter_shop);
    }
}

void as_ft_aboutsub_screen_init(as_screen* screen_p)
{
    as_rect area;

    if(!screen_p)
        return;

    screen_p->set_bg_color(screen_p, DATA_SHEET_BG_COLOR);
    s_as_ft_aboutsub_scr_p = (as_ft_aboutsub_screen*)screen_p;
    area.x = 0;
    area.y = as_app_get_advbar_height();
    area.w = SCR_W;
    area.h = SMALL_TOP_BAR_H;
    as_screen_create_title_bar(screen_p, area, IMG_SMALL_TOP, IMG_TITLE_ABOUT);
    area.x += 1;
    area.w = SCR_W - 2;
    area.y += area.h;
    area.h = SCR_H - BOTTOM_BAR_HEIGHT - area.y;
    as_ft_aboutsub_screen_create_content(screen_p, area);
    area.w = SCR_W;
    area.h = BOTTOM_BAR_HEIGHT;
    area.x = 0;
    area.y = SCR_H - area.h;
    as_ft_aboutsub_screen_create_bottom_bar(screen_p, area);

    if(as_app_get_show_advbar())
    {
        area.x = area.y = 0;
        area.w = SCR_W;
        as_screen_add_advbar(screen_p, area);
    }
}

void as_ft_aboutsub_screen_deinit(as_screen* screen_p)
{
    s_as_ft_aboutsub_scr_p = ASNULL;
}

void as_enter_ft_aboutsub_screen(void)
{
    as_screen_manager* scr_mgr_p = as_get_screen_stack_instance();
    as_screen* scr_p;
    scr_mgr_p->new_and_push_screen(AS_SCREEN_FTABOUT_SUB);
    scr_p = scr_mgr_p->get_cur_screen();

    if(scr_p)
    {
        scr_p->paint_hdlr(scr_p);
        scr_p->flush_hdlr(scr_p);
    }
}